package com.kakavr.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;

import org.joda.time.DateTime;


public class DateTimeUtils {
	private static final String TIME_PATTERN = "yyyy-MM-dd HH:mm";
	private static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
	private static final String Date_PATTERN = "yyyy-MM-dd";
	
	public static String dateToTimePattern(Date date){
		return new SimpleDateFormat(TIME_PATTERN).format(date);
	}
	public static String dateToStringSFM(Date date){
		return new SimpleDateFormat(DATETIME).format(date);
	}
	public static String dateToStringPattern(Date date){
		return new SimpleDateFormat(Date_PATTERN).format(date);
	}
	
	/**
	 * 取本周一的日期
	 * @param date
	 * @return
	 */
	public static Date getThisWeekMonday(Date date) { 
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);  
        // 获得当前日期是一个星期的第几天  
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);  
        if (1 == dayWeek) {  
            cal.add(Calendar.DAY_OF_MONTH, -1);  
        }  
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一  
        cal.setFirstDayOfWeek(Calendar.MONDAY);  
        // 获得当前日期是一个星期的第几天  
        int day = cal.get(Calendar.DAY_OF_WEEK);  
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值  
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
        return cal.getTime();  
    }
	
	/**
	* 得到本周周二日期
	* @return
	*/
	public static Date getWeekTuesday(){
		Calendar cal=new GregorianCalendar();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 1);
        Date last=cal.getTime();
        return last;
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(getThisWeekMonday(DateTime.now().toDate()));
	}

}
